package com.bomberman.items;

import com.bomberman.environment.BombWorld;
import com.bomberman.environment.Man;
import com.bomberman.environment.Tele;
import com.bomberman.environment.Tile;
import java.awt.Color;

import info.gridworld.grid.*;

public class BOOM extends Item
//Explosion/Fire/whatever, created by Bomb, fans out, causes Man to die
{
	Color clr;
	Location startLoc;
	
	public BOOM(BombWorld world, Color Clr)
	{
		super(world);
		clr = Clr;
		setColor(clr);
	}
	
	public void act()
	//burns out after a turn
	{removeSelfFromGrid();}
	
	public void explode(int n)
	//triggered by Bomb, explodes in + pattern, n units
	//bascially, it just moves n units in each ordinal direction, from one point
	{
		startLoc = getLocation();
		
		String asdw = "ASDW";
		for(int i=0; i<4; i++)
			{
			BOOM asdf = ((BOOM) w.getGrid().get(startLoc));
			asdf.startLoc = startLoc;
			asdf.move(asdw.substring(i,i+1), n);
			}
	}
	
	public void move(String desc, int n)
	{
		for(int i=0; i<n; i++)
			move(desc);
	}
	
	public boolean move(String desc)
	//checks and moves
	{
		Location loc = getLocation();
		if(loc==null)
		{	
			w.add(startLoc,this);
			loc = startLoc;
		}
		Location toLoc = w.asdwLoc(desc, loc);
				
		Grid g = w.getGrid();
		if(g.isValid(toLoc))
			{
			moveTo(toLoc);
			return true;
			}
			
		return false;
	}
	
	public void moveTo(Location toLoc)
	//more checks, and moves.
	//all of the checks and interactions between BOOM and others is handled here
	//Bomb - explode
	//Item - ignores, plows thru
	//Rock - 
	{
		Location loc = getLocation();
		
		Grid g = w.getGrid();
		if(emptyLoc(toLoc))
			{
			super.moveTo(toLoc);
			w.add(loc, new BOOM(w, clr));
			return;
			}
		else
			{
				Class asdf = g.get(toLoc).getClass();
				if(asdf.getSuperclass().equals(Item.class))
				{
					super.moveTo(toLoc);
					w.add(loc, new BOOM(w, clr));
					return;
				}
			else
				if(asdf.getSuperclass().equals(Tile.class))
				{
					Tile tile = ((Tile) w.getGrid().get(toLoc));
					if( tile.open && tile.occupied )
					{
						if(true)	//asdf.getClass().equals(Tele.class))
							{
							BOOM boom = new BOOM(w, clr);
							w.add(w.randLoc(), boom);
							((Tele)tile).boomed(boom);
							}
					}
				}
			else
				if(asdf.equals(Man.class))
				{
					BOOM boom = new BOOM(w, clr);
					w.add(w.randLoc(), boom);
					boom.got( (Man) g.get(toLoc) );
				}
			else if(asdf.equals(Bomb.class))
					w.Bombs.get(w.Bombs.indexOf(((Bomb) g.get(toLoc))))
						.explode();
				
				return;
			}
	}
	
	public boolean validLoc(Location Loc)
	{
		int x = Loc.getRow();
		int y = Loc.getCol();
		int X = w.getGrid().getNumRows();
		int Y = w.getGrid().getNumCols();
		
		return (x>-1 && x<X) && (y>-1 && y<Y);
	}
	
	public boolean emptyLoc(Location Loc)
	{
		if(validLoc(Loc))
			return w.getGrid().get(Loc) == null;
		return false;
	}
	
	public void use() 
	//BOOM is treated like an item - a Man walks into it, picks it up, and dies
	{P.dead();}
}